// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   AppConfigurator.java

package com.boci.common.util;

import com.boci.clientapp.main.ClientAppLoader;
import java.io.*;
import java.util.*;

// Referenced classes of package com.boci.common.util:
//            PropertiesUtil

public class AppConfigurator
{

    public static AppConfigurator getInstance()
        throws Exception
    {
        if(instance == null)
            instance = new AppConfigurator();
        return instance;
    }

    private AppConfigurator()
        throws Exception
    {
        appConfigProperties = new Properties();
        appUIConfigProperties = new Properties();
        try
        {
            appConfigProperties = PropertiesUtil.loadPropFromJar("/com/boci/common/resources/config/clientapp.config");
            strClientAppUIConfigPath = (new StringBuilder()).append(ClientAppLoader.getTempPath()).append(System.getProperty("file.separator")).append("app_ui.config").toString();
            File appUIFile = new File(strClientAppUIConfigPath);
            if(appUIFile.exists())
            {
                appUIConfigProperties = PropertiesUtil.loadPropFromFile(strClientAppUIConfigPath);
            } else
            {
                appUIConfigProperties = PropertiesUtil.loadPropFromJar("/com/boci/common/resources/config/app_ui.config");
                PropertiesUtil.storeProperties(appUIConfigProperties, strClientAppUIConfigPath);
            }
            if(ClientAppLoader.isStandalone())
            {
                File proxyCfgFile = new File((new StringBuilder()).append(ClientAppLoader.getAppPath()).append("/proxy.config").toString());
                if(proxyCfgFile.exists())
                    proxyConfigProperties = PropertiesUtil.loadPropFromFile(proxyCfgFile.getAbsolutePath());
            }
        }
        catch(IOException e)
        {
            throw e;
        }
    }

    public String getProxyConfig(String pKey)
    {
        if(proxyConfigProperties == null)
            return null;
        else
            return proxyConfigProperties.getProperty(pKey);
    }

    public void setProxyConfig(String pKey, String pValue)
    {
        if(proxyConfigProperties == null)
            proxyConfigProperties = new Properties();
        proxyConfigProperties.setProperty(pKey, pValue);
    }

    public void saveProxyConfig()
        throws Exception
    {
        String strPath = (new StringBuilder()).append(ClientAppLoader.getAppPath()).append("/proxy.config").toString();
        FileOutputStream fout = new FileOutputStream(strPath);
        proxyConfigProperties.store(fout, null);
        fout.close();
    }

    public static Properties getAppConfigProperties()
    {
        return appConfigProperties;
    }

    public void saveProperties(String pFilePath, HashMap pDataHashMap)
        throws IOException
    {
        Properties tmpProperties;
        FileOutputStream outTmp;
        if(!ClientAppLoader.isStandalone())
            return;
        if(pDataHashMap == null || pDataHashMap.isEmpty())
            return;
        tmpProperties = new Properties();
        outTmp = null;
        String strKey;
        String strValue;
        for(Iterator itr = pDataHashMap.keySet().iterator(); itr.hasNext(); tmpProperties.setProperty(strKey, strValue))
        {
            strKey = (String)itr.next();
            strValue = (String)pDataHashMap.get(strKey);
        }

        try
        {
            outTmp = new FileOutputStream(pFilePath);
            tmpProperties.store(outTmp, null);
        }
        catch(FileNotFoundException e)
        {
            throw e;
        }
        catch(IOException e)
        {
            throw e;
        }finally{
        if(outTmp != null)
            outTmp.close();
        }
        return;
    }

    public String getAppConfig(String pKey)
    {
        if(appConfigProperties == null || appConfigProperties.isEmpty())
            return null;
        String strValue = appConfigProperties.getProperty(pKey);
        if(strValue == null)
            return null;
        if(strValue.startsWith("%HOME%"))
        {
            strValue = strValue.replaceFirst("%HOME%", "");
            strValue = (new StringBuilder()).append(ClientAppLoader.getUserHome()).append(strValue).toString();
        } else
        if(strValue.startsWith("%TEMP%"))
        {
            strValue = strValue.replaceFirst("%TEMP%", "");
            strValue = (new StringBuilder()).append(ClientAppLoader.getSysTmp()).append(strValue).toString();
        } else
        if(strValue.startsWith("%APP%"))
        {
            strValue = strValue.replaceFirst("%APP%", "");
            strValue = (new StringBuilder()).append(ClientAppLoader.getAppPath()).append(strValue).toString();
        }
        return strValue.trim();
    }

    public boolean getBoolean(String pKey)
    {
        String strVal = getAppConfig(pKey);
        if(strVal == null)
            return false;
        return strVal.toUpperCase().equals("Y");
    }

    public int getInt(String pKey, int pDefaultValue)
    {
        String strVal = getAppConfig(pKey);
        int intVal = pDefaultValue;
        try
        {
            intVal = Integer.parseInt(strVal);
        }
        catch(Exception e)
        {
            return pDefaultValue;
        }
        return intVal;
    }

    public String[] getStrings(String pKey)
    {
        String strRtnVal[] = {
            ""
        };
        ArrayList tmpList = new ArrayList();
        String strVal = getAppConfig(pKey);
        if(strVal == null || strVal.length() == 0)
            return strRtnVal;
        for(StringTokenizer st = new StringTokenizer(strVal, ","); st.hasMoreTokens(); tmpList.add(st.nextToken()));
        strRtnVal = (String[])(String[])tmpList.toArray(strRtnVal);
        return strRtnVal;
    }

    public String getAppUIConfig(String pKey)
    {
        if(appUIConfigProperties == null || appUIConfigProperties.isEmpty())
            return null;
        else
            return appUIConfigProperties.getProperty(pKey);
    }

    public static Properties getAppUIConfigProperties()
    {
        return appUIConfigProperties;
    }

    public String getStrAppUIConfigPath()
    {
        return strClientAppUIConfigPath;
    }

    private static Properties appConfigProperties;
    private static Properties appUIConfigProperties;
    private String strClientAppUIConfigPath;
    private static Properties proxyConfigProperties;
    private static AppConfigurator instance;
}
